<?php
/**
 * Eren Framework
 * 
 * Request Params Operate
 * 
 * @category System
 * @author Manu Soros Leu
 * @copyright manusoros@gmail.com
 */

class Eren_Request_Param
{
    
    /**
     * Instance of this
     * 
     * @var Eren_View
     */
    private static $_instance = NULL;
    
    /**
     * 
     * Enter description here ...
     * @var array
     */
    private $_params = array();
    
    /**
     * Realize Singleton Model
     */
    private function __construct(){}
    
    /**
     * Get Instance
     * 
     * @return this
     */
    public static function getInstance()
    {
        if(self::$_instance == NULL){
            self::$_instance = new Eren_Request_Param;
            self::$_instance->_params = $_GET;
            self::$_instance->_initParam();
        }
        return self::$_instance;
    }
    
    /**
     * Init params from QueryString
     */
    private function _initParam()
    {
        $this->_params = $_GET;
        $routerString = Eren_Request_URL::getRouterString();
        $routerString = explode('/', trim($routerString, '/'));
        for($i=0;$i<count($routerString);$i++){
            if(isset($routerString[$i+1])){
                $this->_params[$routerString[$i]] = $routerString[$i+1];
            }else{
                $this->_params[$routerString[$i]] = NULL;
            }
            $i++;
        }
    }
    
    /**
     * Get Param from $_GET
     * 
     * @param string $key
     * @param string $default
     * @throws Eren_Request_Exception
     * @return string
     */
    public function getParam($key, $default = NULL)
    {
        if(is_array($key) || empty($key)){
            throw new Eren_Request_Exception('The first param of getParam cant be array or null');
        }
        $key = (string) $key;
        if(isset($this->_params[$key])){
            return $this->_params[$key];
        }
        return $default;
    }
    
    /**
     * Get All Param from $_GET
     * 
     * @return array
     */
    public function getParams()
    {
        return $this->_params;
    }
    
    /**
     * Get Param from $_POST
     * 
     * @param string $key
     * @param string $default
     * @throws Eren_Request_Exception
     * @return string
     */
    public function getPost($key, $default = NULL)
    {
        if(is_array($key) || empty($key)){
            throw new Eren_Request_Exception('The first param of getPost cant be array or null');
        }
        $key = (string) $key;
        if(isset($_POST[$key])){
            return $_POST[$key];
        }
        return $default;
    }
    
    /**
     * Get All Param from $_POST
     * 
     * @return array
     */
    public function getPosts()
    {
        return $_POST;
    }
}